from flask import Flask
from flask import request
from flask import render_template
from flask import jsonify
from jieba.analyse import  extract_tags
import string
import utils
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("main.html")

@app.route('/world')
def hello_world1():
    return render_template("fmain.html")

@app.route("/c1")
def get_c1_data():
    data = utils.get_c1_data()
    return jsonify({"confirm":data[0],"suspect":data[1],"heal":data[2],"dead":data[3]})

@app.route("/c2")
def get_c2_data():
    res = []
    for tup in utils.get_c2_data():

        res.append({"name":tup[0],"value":int(tup[1])})
    return jsonify({"data":res})



@app.route("/c31")
def get_c3_data1():
    data = utils.get_c3_data1()
    city = []
    nowConfirm = []
    confirm= []
    suspect= []
    dead= []
    heal= []

    for k, v ,a,b,c,d in data:
        city.append(k)
        nowConfirm.append(int(v))
        confirm.append(int(a))
        suspect.append(int(b))
        dead.append(int(c))
        heal.append(int(d))
    return jsonify({"city": city, "nowConfirm": nowConfirm,"confirm":confirm,"suspect":suspect,"dead":dead,"heal":heal})


@app.route("/l1")
def get_l1_data():
    data = utils.get_l1_data()
    day,confirm,suspect,heal,dead = [],[],[],[],[]
    for a,b,c,d,e in data[7:]:
        day.append(a.strftime("%m-%d")) #a是datatime类型
        confirm.append(b)
        suspect.append(c)
        heal.append(d)
        dead.append(e)
    return jsonify({"day":day,"confirm": confirm, "suspect": suspect, "heal": heal, "dead": dead})

@app.route("/l2")
def get_l2_data():
    data = utils.get_l2_data()
    day, confirm_add, suspect_add = [], [], []
    for a, b, c in data[7:]:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        confirm_add.append(b)
        suspect_add.append(c)
    return jsonify({"day": day, "confirm_add": confirm_add, "suspect_add": suspect_add})

@app.route("/l3")
def get_l3_data():
    res = []
    for tup in utils.get_l3_data():
        res.append({"value":int(tup[1]), "name":tup[0]})
    return jsonify({"data":res})


@app.route("/r1")
def get_r1_data():
    data = utils.get_r1_data()
    city = []
    confirm = []
    for k,v in data:
        city.append(k)
        confirm.append(int(v))
    return jsonify({"city": city, "confirm": confirm})


@app.route("/r2")
def get_r2_data():
    data = utils.get_r2_data() #格式 (('民警抗疫一线奋战16天牺牲1037364',), ('四川再派两批医疗队1537382',)
    d = []
    for i in data:
        k = i[0].rstrip(string.digits)  # 移除热搜数字
        v = i[0][len(k):]  # 获取热搜数字
        ks = extract_tags(k)  # 使用jieba 提取关键字
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": v})
    return jsonify({"kws": d})

@app.route("/r3")
def get_r3_data():
    res = []
    for tup in utils.get_r3_data():
        res.append({"value":int(tup[1]), "name":tup[0]})
    return jsonify({"data":res})



'''
    世界部分
'''
@app.route("/fc1")
def get_fc1_data():
    data = utils.get_fc1_data()
    return jsonify({"nowConfirm":data[0],"nowConfirmAdd":data[1],"heal":data[2],"dead":data[3]})


@app.route("/fc2")
def get_fc2_data():
    res = []
    for tup in utils.get_fc2_data():
        # print(tup)
        res.append({"name": tup[0], "value": int(tup[1])})
        res.append({"name":"中国","value":36})
    return jsonify({"data": res})


@app.route("/fc3")
def get_fc3_data():
    data = utils.get_fc3_data()
    day,deadRate,healRate = [], [],[]
    for a, b, c in data:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        deadRate.append(b)
        healRate.append(c)
    return jsonify({"day": day, "deadRate": deadRate, "healRate": healRate})


@app.route("/fl1")
def get_fl1_data():
    data = utils.get_fl1_data()
    day, confirm, heal, dead, newAddConfirm = [], [], [], [], []
    for a, b, c, d, e in data:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        confirm.append(b)
        heal.append(c)
        dead.append(d)
        newAddConfirm.append(e)
    return jsonify({"day": day,"confirm": confirm,"heal": heal,
                    "dead": dead,"newAddConfirm": newAddConfirm})


@app.route("/fl2")
def get_fl2_data():
    data = utils.get_fl2_data()
    city = []
    confirm = []
    for k,v in data:
        city.append(k)
        confirm.append(int(v))
    return jsonify({"city": city, "confirm": confirm})

@app.route("/fl3")
def get_fl3_data():
    data = utils.get_fl3_data()
    city = []
    nowConfirm = []
    dead = []
    for k, v,c in data:
        city.append(k)
        nowConfirm.append(int(v))
        dead.append(int(c))
    return jsonify(
        {"city": city, "nowConfirm": nowConfirm, "dead": dead})


@app.route("/fr1")
def get_fr1_data():  # 全球累计趋势
    res = []
    for tup in utils.get_fr1_data():
        res.append({"value": int(tup[1]), "name": tup[0]})
    return jsonify({"data": res})


@app.route("/fr2")
def get_fr2_data():
    res = []
    for tup in utils.get_fr2_data():
        res.append({"value": int(tup[1]), "name": tup[0]})
    return jsonify({"data": res})


@app.route("/fr3")
def get_fr3_data():
    data = utils.get_fr3_data()  # 格式 (('民警抗疫一线奋战16天牺牲1037364',), ('四川再派两批医疗队1537382',)
    d = []
    for i in data:
        k = i[0].rstrip(string.digits)  # 移除热搜数字
        v = i[0][len(k):]  # 获取热搜数字
        ks = extract_tags(k)  # 使用jieba 提取关键字
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": v})
    return jsonify({"kws": d})
    
    
@app.route("/time")
def get_time():
    return utils.get_time()



if __name__ == '__main__':
    
    app.run()
